package com.xiu.controller;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.xiu.mapper.ExamUserMapper;
import com.xiu.util.ImportExcelUtil;

@Controller
@RequestMapping(value = "/exam")  
public class FileUploadController {
	@Autowired
	ExamUserMapper examUserMapper;
	
	@RequestMapping(value="/upload",method=RequestMethod.GET)
	public String index(){
		return "exam/upload";
	}
	
	@RequestMapping(value = "/fileUpload")  
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpSession session, ModelMap model,HttpServletRequest request,HttpServletResponse response) throws Exception {  
		response.setCharacterEncoding("utf-8");  
        response.setContentType("text/html;charset=utf-8");
        String path = session.getServletContext().getRealPath("upload");  
        String fileName = file.getOriginalFilename();  
        File targetFile = new File(path, fileName);  
        System.out.println(targetFile);
        if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }  
        //保存  
        try {  
            file.transferTo(targetFile);  
            System.out.println("进来了么？");
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        
       ImportExcelUtil e = new ImportExcelUtil(); 
    	//e.ImportExcel(path+"\\"+fileName);
    	FileInputStream file2 = new FileInputStream(path+"\\"+fileName);
    	e.insertExcel(examUserMapper, file2);
    	model.addAttribute("message", "Upload successfully.");  
    	
        return "exam/upload";  
    }  
}
